<?php

require_once 'LogManager.php';
	class LogView{
		
		public function checkUsers($userInput, $userPword){
			
			$conn=oci_connect("naiad","1234","localhost/XE");
			if(!$conn){
				$err = "Connection error!". var_dump(oci_error());
				echo '<script> alert("'.$err.'") </script>';
				die();
				oci_close($conn);
			}
			else{
				$flag = 0;
				$s = oci_parse($conn, "select * from users");
				oci_execute($s, OCI_DEFAULT);
				while(oci_fetch($s)){
					if(($userPword==oci_result($s,'PASSWORD'))&&((strlen(trim($userInput)))>0)&&
					($userInput==oci_result($s, 'USERNAME'))){
					$flag=1;
					break;
					}
				}
	
				if($flag==1) header("Location: main.html?addCheckin.php");
				else{
					header("Location:index.php?message=error");
				}
				oci_close($conn);
			}	
		}
		
		
		public function checkDuplicate($snum){
			
			$conn=oci_connect("naiad","1234","localhost/XE");
			if(!$conn){
				$err = "Connection error!". var_dump(oci_error());
				echo '<script> alert("'.$err.'") </script>';
				die();
				oci_close($conn);
			}
			else{
				$s = oci_parse($conn, "select * from CHECK_IN");
				oci_execute($s, OCI_DEFAULT);
				while(oci_fetch($s)){
					if($snum==oci_result($s,'STUDENTNO')){
						return 1;
						break;
					}
				}
				oci_close($conn);
			}	
		}
		
		// function showMessage($flag){
			// $try=0;
			// if($flag==1){
				// echo '<script>alert("Succcessfully Added")</script>';
				// $try=1;	
				// continue;
			// }else{
				// echo  '<script>alert("Error in insertion.")</script>';
				// #$_Session['alert']="Error in insertion!";
			// }
			// if($try==1)header("Location: main.html");
			
			
		// }

			function showMessage($flag) {
				if($flag==1){
					$msg = "Successfully Added!";
					$destination = "front.html";
				}else{
					$msg = "Error in insertion!";
					$destination = "logForm.php";
				}
			
				echo("<html>\n");
				echo("<head>\n");
				echo("<title>System Message</title>\n");
				echo("<meta http-equiv=\"Content-Type\" content=\"text/html;
				charset=iso-8859-1\">\n");

				echo("<script language=\"JavaScript\" type=\"text/JavaScript\">\n");
				echo("alert('$msg');\n");
				echo("window.location = ('$destination');\n");
				echo("</script>\n");
				echo("</head>\n");
				echo("<body>\n");
				echo("</body>\n");
				echo("</html>\n");
				exit;
			}

		
		
		function insertLog($snum,$date_in,$term,$room,$date_out,$lname,$mname,$fname){	
			
			$date_in = strtoupper($date_in);
			$term = strtoupper($term);
			$room = strtoupper($room);
			$lname = strtoupper($lname);
			$mname = strtoupper($mname);
			$fname = strtoupper($fname);
			$flag = LogManager::insertLog($snum,$date_in,$term,$room,$date_out,$lname,$mname,$fname);
			
			$newObj = new LogView;
			$newObj->showMessage($flag);
			
		}
	}
?>
